package com.example.demo.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableKnife4j
//@MapperScan("com.example.demo.dao.UserDao")
public class SwaggerConfig extends WebMvcConfigurationSupport {
    //配置swagger的docket的bean实例
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//用来创建该Api的基本信息(这些基本信息会展现在文档页面中)。
//                .pathMapping("/")
                .select()//需要扫描的控制类包 创建一个构建
                .apis(RequestHandlerSelectors.basePackage( "com.example.demo.controller"))//
                .paths(PathSelectors.any())//允许根据路径映射定义应包含那个控制器的方法
                .build();
    }
    //配置swagger信息、
    private ApiInfo apiInfo(){
        Contact contact=new Contact("ycplayer","null", "null");
        return new ApiInfoBuilder()
                .title("接口总览")
                .description("接口测试")
                .version("1.0")
                .contact(contact)
                .build();
    }
    /***
     * 防止@EnableMvc把默认的静态资源路径覆盖了，手动设置的方式
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry){
        //配置拦截器访问静态资源
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
